#!/bin/bash

# for FileMaker 17.0.1

# replace path to app: /Users/cs/Desktop/Test
# replace name of app: test.app
# replace name of certificate: "Developer ID Application: Christian Schmitz Software GmbH"

# Special characters like spaces need to be escaped in paths. e.g. space character gets a backslash before the space.

cd /Users/cs/Desktop/Test/Test.app/Contents

# delete old signature, if existing
rm -Rfv _CodeSignature 

# delete unnecessary files
rm -fv Resources/FM12Dict.icns Resources/FM12Label.icns Resources/FM12Plug.icns Resources/IDB_POINTERBREAKPOINTTemplate.pdf Resources/IDB_CLEARPOINTERBREAKPOINTTemplate.pdf Resources/IDB_POINTERTemplate.pdf Resources/IDB_BREAKPOINTTemplate.pdf Resources/IDB_CLEARPOINTERTemplate.pdf

# fix the frameworks
# rm commands to make sure we recreate things correctly.
cd Frameworks/

cd Cloud.framework
rm -Rfv _CodeSignature Cloud Resources Versions/Current
ln -s Versions/A/Resources Resources
ln -s Versions/A/Cloud Cloud
ln -s A Versions/Current
cd ..

cd DBEngine.framework
rm -Rfv _CodeSignature DBEngine Resources Versions/Current
ln -s Versions/A/Resources Resources
ln -s Versions/A/DBEngine DBEngine
ln -s A Versions/Current
cd ..

cd FMEngine.framework
rm -Rfv _CodeSignature FMEngine Resources Versions/Current
ln -s Versions/A/Resources Resources
ln -s Versions/A/FMEngine FMEngine
ln -s A Versions/Current
cd ..

cd FMWrapper.framework
rm -Rfv _CodeSignature FMWrapper Resources Versions/Current
rm -rf Versions/A/Headers
ln -s Versions/A/Resources Resources
ln -s Versions/A/FMWrapper FMWrapper
ln -s A Versions/Current
cd ..

cd Support.framework
rm -Rfv _CodeSignature Resources Support Versions/Current
ln -s Versions/A/Resources Resources
ln -s Versions/A/Support Support
ln -s A Versions/Current
cd ..


# now we sign all the parts
cd /Users/cs/Desktop/Test/


# remove extended attributes, if present
xattr -cr test.app

# sign from inside to outside
codesign -f -vvvv --options runtime -s "Developer ID Application: Christian Schmitz Software GmbH" test.app/Contents/Frameworks/*.framework
codesign -f -vvvv --options runtime -s "Developer ID Application: Christian Schmitz Software GmbH" test.app/Contents/Frameworks/*.dylib
codesign -f -vvvv --options runtime -s "Developer ID Application: Christian Schmitz Software GmbH" test.app/Contents/XPCServices/*.xpc
codesign -f -vvvv --options runtime -s "Developer ID Application: Christian Schmitz Software GmbH" test.app/Contents/MacOS/Runtime
codesign -f -vvvv --options runtime -s "Developer ID Application: Christian Schmitz Software GmbH" test.app

# optional with entitlements: 
# codesign -f -vvvv --options runtime -s "Developer ID Application: Christian Schmitz Software GmbH" --entitlements /path/to/Runtime.entitlements test.app

# also sign any extensions you use
codesign -f -vvvv --options runtime -s "Developer ID Application: Christian Schmitz Software GmbH" Extensions/*.fmplugin

# test it
codesign -v -v test.app
spctl -v -a test.app

# needs to write accepted for success!


# Common problems:
#
# * Wrong name for certificate
# * Bad/Old certificate
# * Missing private key
# * Bad path above pointing to app, not to Contents folder inside app.
# * Special characters in the path may need escaping.
# * Developer not reading output in Terminal!

